/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agent;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import util.ThreadMBeanRegister;

/**
 *
 * @author alan
 */
public class AgentMBean {

    private static JMXServiceURL JMXURL;
    private static JMXConnector JMXConn;
    private static MBeanServerConnection JMXSC;

    public AgentMBean() {
    }

    public static MBeanServerConnection makeAgent(String ip) {
        try {
            JMXURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + ip + ":17503/server");
            JMXConn = JMXConnectorFactory.connect(JMXURL, null);
            JMXSC = JMXConn.getMBeanServerConnection();
            return JMXSC;
        } catch (IOException ex) {
            return null;
        }
    }

    public static void unmakeMBean(String ipNMS) throws MalformedObjectNameException, UnknownHostException {
        try {
            ObjectName mbean = null;
            if (System.getProperty("os.name").contains("Windows")) {
                mbean = new ObjectName("tcc:type=MACHINEWINDOWS,ip=" + Inet4Address.getLocalHost().getHostAddress() + ",nome=" + Inet4Address.getLocalHost().getHostName());
            } else {
                if (System.getProperty("os.name").contains("Linux")) {
                    mbean = new ObjectName("tcc:type=MACHINELINUX,ip=" + Inet4Address.getLocalHost().getHostAddress() + ",nome=" + Inet4Address.getLocalHost().getHostName());
                }
            }
            JMXSC.unregisterMBean(mbean);
        } catch (InstanceNotFoundException ex) {
            Logger.getLogger(AgentMBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MBeanRegistrationException ex) {
            Logger.getLogger(AgentMBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AgentMBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static void makeMBean(String ipNMS) throws MalformedObjectNameException, InterruptedException, UnknownHostException {
        makeAgent(ipNMS);
        String ip = Inet4Address.getLocalHost().getHostAddress();
        String hostName = Inet4Address.getLocalHost().getHostName();
        Thread registerMBean;
        ObjectName mbean = null;
        if (System.getProperty("os.name").contains("Windows")) {
            mbean = new ObjectName("tcc:type=MACHINEWINDOWS,ip=" + ip + ",nome=" + hostName);
            registerMBean = new Thread(new ThreadMBeanRegister(mbean, JMXSC, ipNMS));
            registerMBean.start();
        } else {
            if (System.getProperty("os.name").contains("Linux")) {
                mbean = new ObjectName("tcc:type=MACHINELINUX,ip=" + Inet4Address.getLocalHost().getHostAddress() + ",nome=" + Inet4Address.getLocalHost().getHostName());
                registerMBean = new Thread(new ThreadMBeanRegister(mbean, JMXSC, ipNMS));
                registerMBean.start();
            }
        }
    }
}
